import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
import { NO_RESET_WHITE_LIST } from '@/constants'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/admin/index',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/admin',
    component: Layout,
    redirect: '/admin/index',
    name: 'Admin',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/Admin/index.vue'),
        name: 'AdminDemo',
        meta: {
          title: '管理员管理',
          icon: 'wpf:administrator',
          affix: true
        }
      }
    ]
  },
  {
    path: '/certificate',
    component: Layout,
    name: 'Certificate',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/Certificate/index.vue'),
        name: 'CertificateDemo',
        meta: {
          title: '证书管理',
          icon: 'material-symbols:contact-phone'
        }
      }
    ]
  },
  {
    path: '/child',
    component: Layout,
    name: 'Child',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/Child/index.vue'),
        name: 'ChildDemo',
        meta: {
          title: '儿童管理',
          icon: 'material-symbols:contact-phone'
        }
      }
    ]
  },
  {
    path: '/material',
    component: Layout,
    name: 'Material',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/Material/index.vue'),
        name: 'MaterialDemo',
        meta: {
          title: '物资管理',
          icon: 'streamline-freehand-color:customer-action-complaint'
        }
      }
    ]
  },
  {
    path: '/donate',
    component: Layout,
    name: 'Donate',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/Donate/index.vue'),
        name: 'DonateDemo',
        meta: {
          title: '物资捐赠管理',
          icon: 'fluent-mdl2:event-date'
        }
      }
    ]
  },
  {
    path: '/donateRecord',
    component: Layout,
    name: 'DonateRecord',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/DonateRecord/index.vue'),
        name: 'DonateRecordDemo',
        meta: {
          title: '物资捐赠记录',
          icon: 'hugeicons:tools'
        }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    name: 'User',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/User/index.vue'),
        name: 'UserDemo',
        meta: {
          title: '用户管理',
          icon: 'ix:user-group'
        }
      }
    ]
  },
  {
    path: '/rescue',
    component: Layout,
    name: 'Rescue',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/Rescue/index.vue'),
        name: 'RescueDemo',
        meta: {
          title: '救助管理',
          icon: 'icon-park-twotone:washing-machine'
        }
      }
    ]
  },
  {
    path: '/rescueRecord',
    component: Layout,
    name: 'RescueRecord',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/RescueRecord/index.vue'),
        name: 'RescueRecordDemo',
        meta: {
          title: '救助记录',
          icon: 'hugeicons:tools'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !NO_RESET_WHITE_LIST.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
